/* Name: CreateProbeBtn
 * Author: John Gideon
 * Date: 5-15-12
 * Description: Button used to create a probe in game when a planet is selected.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

public class CreateProbeBtn extends Button {
	private int width;
	private int height;
	private Boolean visible = false;
	private Boolean creating = false;
	
	// Creates a probe button at the given location, with the given dimensions
	public CreateProbeBtn(int px, int py, int pwidth, int pheight) {
		super(px, py, pwidth, pheight);
		Game.create((Item)this, 1000000); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking
		width = pwidth;
		height = pheight;
	}
	
	// Called when button is pressed - sets visual elements, and plays sound.
	protected void pressed() {
		Sound.effect(Sound.EFFECT_CLICK);
		Planet pid = GameScreen.getSelectedPlanet();
		if (pid != null) {
			creating = true;
			GameScreen.setButtonsVisible(false);
		} else {
			setSelected(false);
		}
	}
	
	// Called when button is released
	protected void released() {
		creating = false;
	}
	
	// Draws actual button on screen - called whenever a new frame is drawn
	public void draw() {
		Graphics2D gh = Game.getGraphicsHandle();
		Planet pid = GameScreen.getSelectedPlanet();
		if (pid == null) {creating = false;}
		if (creating) {
			float endX = (float)ViewControl.mouseX();
			float endY = (float)ViewControl.mouseY();
			float mag = (float)Math.sqrt(Math.pow(endX - pid.x,2) + Math.pow(endY - pid.y,2));
			float orePrice = mag/300f;
			float fuelPrice = mag/200f;
			if(mag < (float)Probe.minDistance || mag > (float)Probe.maxDistance){
				gh.setColor(Color.RED);
			}
			else if(orePrice > GameScreen.totalOre || fuelPrice > GameScreen.totalFuel){
				gh.setColor(Color.RED);
			}
			else{
				gh.setColor(Color.BLUE);
			}
			gh.setStroke(new BasicStroke(5f));
			final double ARROW_SIZE = 30.0;
			final double ARROW_DIR_OFFSET = 0.4;
			double dir = Math.atan2(pid.y-ViewControl.mouseY(),pid.x-ViewControl.mouseX());
			int dx1 = (int) (ARROW_SIZE*Math.cos(dir+ARROW_DIR_OFFSET));
			int dy1 = (int) (ARROW_SIZE*Math.sin(dir+ARROW_DIR_OFFSET));
			int dx2 = (int) (ARROW_SIZE*Math.cos(dir-ARROW_DIR_OFFSET));
			int dy2 = (int) (ARROW_SIZE*Math.sin(dir-ARROW_DIR_OFFSET));
			gh.drawLine(pid.x, pid.y, ViewControl.mouseX(), ViewControl.mouseY());
			gh.drawLine(ViewControl.mouseX(), ViewControl.mouseY(), ViewControl.mouseX()+dx1, ViewControl.mouseY()+dy1);
			gh.drawLine(ViewControl.mouseX(), ViewControl.mouseY(), ViewControl.mouseX()+dx2, ViewControl.mouseY()+dy2);
			String oreString = String.format("Ore: %.2f", orePrice);
			String fuelString = String.format("Fuel: %.2f", fuelPrice);
			gh.setFont(new Font("Serif", Font.PLAIN, 20));
			gh.setColor(Color.WHITE);
			gh.drawString(oreString,ViewControl.mouseX() + 5, ViewControl.mouseY() - 5);
			gh.drawString(fuelString,ViewControl.mouseX() + 5, ViewControl.mouseY() + 20);
		} 
		if (visible) {
			if (btnDrawState == Button.BTN_STATE_OFF) {gh.setColor(Color.GRAY);}
			else if (btnDrawState == Button.BTN_STATE_HOVER) {gh.setColor(Color.LIGHT_GRAY);}
			else {gh.setColor(Color.DARK_GRAY);}
			gh.fillRoundRect(x, y, width, height, 15, 15);
			gh.setFont(new Font("Serif", Font.PLAIN, 20));
			gh.setColor(Color.BLACK);
			String text = "Create Probe";
			FontRenderContext frc = gh.getFontRenderContext();
			Rectangle2D rect = gh.getFont().getStringBounds(text, frc);
			gh.drawChars(text.toCharArray(), 0, text.length(), (int)(x + width/2 - rect.getWidth()/2), 
					(int)(y + height/2 + rect.getHeight()/2)-5);
		}
	}
	
	public void update() {if (visible) {updateBtn();}}
	
	// Called when the left mouse button is pressed on the button
	public void leftMousePress() {
		if (visible) {
			leftMousePressBtn();
		}
		if (creating) { // Shows the path the probe will follow, and a cost
			Planet pid = GameScreen.getSelectedPlanet();
			float endX = (float)ViewControl.mouseX();
			float endY = (float)ViewControl.mouseY();
			float mag = (float)Math.sqrt(Math.pow(endX - pid.x,2) + Math.pow(endY - pid.y,2));
			float orePrice = mag/300f;
			float fuelPrice = mag/200f;
			if(mag > (float)Probe.minDistance && mag < (float)Probe.maxDistance && orePrice < GameScreen.totalOre && fuelPrice < GameScreen.totalFuel){
				new Probe(GameScreen.getSelectedPlanet(), ViewControl.mouseX(), ViewControl.mouseY());
				creating = false;
				setSelected(false);
				GameScreen.setButtonsVisible(true);
				Planet.removeFuel(fuelPrice);
				Planet.removeOre(orePrice);
			}
		}
	}
	
	public void leftMouseRelease() {if (visible) {leftMouseReleaseBtn();}}
	
	// Cancels selection
	public void rightMousePress() {
		if (creating) {
			creating = false;
			setSelected(false);
			GameScreen.setButtonsVisible(true);
		}
	}
	
	// Used to hide, and unhide the button
	public void setVisible(Boolean val) {
		visible = val;
		if (visible == false) {
			setSelected(false);
		}
	}
}